//
//  LandmarkList.swift
//  demo
//
//  Created by raymond on 2022/5/9.
//

import SwiftUI

struct LandmarkList: View {
  @EnvironmentObject var modelData: ModelData
  @State private var showFavoritesOnly: Bool = false
  
  var filteredLandmarks: [Landmark] {
    modelData.landmarks.filter { landmark in
      !showFavoritesOnly || landmark.isFavorite
    }
  }
  
  var body: some View {
    NavigationView {
      List {
        Toggle(isOn: $showFavoritesOnly){
          Text("FavoritesOnly")
        }
        /*
         To combine static and dynamic views in a list, or to combine two or more different groups of dynamic views, use the ForEach type instead of passing your collection of data to List.
         */
        ForEach(filteredLandmarks) { landmark in
          NavigationLink {
            LandmarkDetail(landmark: landmark)
          } label: {
            LandmarkRow(landmark: landmark)
          }
        }.navigationTitle("Landmarks")
      }
    }
  }
}

struct LandmarkList_Previews: PreviewProvider {
  static var modelData: ModelData = ModelData()
  static var previews: some View {
    ForEach(["iPhone SE (2nd generation)", "iPhone 12"], id: \.self)
    { deviceName in LandmarkList().environmentObject(modelData).previewDevice(PreviewDevice(rawValue: deviceName))
    }
  }
}
